//
//  recorTwoViewController.m
//  EasyToTesco
//
//  Created by ym on 16/3/28.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "recorTwoViewController.h"
#import "AFNetworking.h"
#import "recJiFenTableViewCell.h"
#import "ReadLoginData.h"
#import "selectView.h"
#import "moneyTableViewCell.h"
@interface recorTwoViewController ()<UITableViewDelegate, UITableViewDataSource,SelectViewDelegate>
{
    NSInteger orderstate;
    BOOL BtnState;

}


@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, copy)NSString *username;
@property (nonatomic, strong)NSMutableArray *dataArr;

@property (nonatomic, strong)NSMutableArray *moneyArr;
@property (nonatomic, strong)NSMutableArray *huodongArr;

@end

#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define NORMWIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
#define NORMHEIGHT [[UIScreen mainScreen] bounds].size.height / 667
#define FRAME [[UIScreen mainScreen] bounds]
@implementation recorTwoViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.dataArr = [@[]mutableCopy];
    self.huodongArr = [@[]mutableCopy];
    self.moneyArr = [@[]mutableCopy];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addMyOrderUI];
    [self addUsertopUI];
    [self getdata];
    [self data];
    [self addMyOrdertableview];
}
- (void)addMyOrderUI {
    
    selectView *selectViewaa =[[selectView alloc] initWithFrame:CGRectMake(0, HEIGHT*0.1, WIDTH, HEIGHT*0.06)];
    
    selectViewaa.delegate = self;
    
    [self.view addSubview:selectViewaa];
    
}



- (void)selectBtnClick:(UIButton *)button{
       orderstate = button.tag;
    
        if (button.tag == 1000) {
            BtnState = YES;
            // 现金
            [self MyOrderDataSources];
        } else if (button.tag == 1001) {
            BtnState = NO;
            //积分
            
            
            [self data];
    
        } else {
            BtnState = NO;
            // 活动
            [self MyOrderGethfordDataSources];
        }
    
    
    
}

// 现金

- (void)MyOrderDataSources{
    // 现金
    
    AFHTTPSessionManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = _username;
    
    NSString *fkfs = @"2";
    
    NSDictionary *param = @{@"username":username,
                            @"usertype":@1,
                            @"fkfs":@2,
                            @"p":@1};
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {

        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
       NSLog(@"moneyArr--%@", string);
        
        self.moneyArr = [self toArrayOrNSDictionary:responseObject];
        
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}


- (void)MyOrderGethfordDataSources{
    // 活动
    
    
    AFHTTPSessionManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = _username;

    
    NSDictionary *param = @{@"username":_username,
                            @"usertype":@1,
                            @"fkfs":@4,
                            @"p":@1};
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
    
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
    NSLog(@"huodongArr--%@", string);
        
        self.huodongArr = [self toArrayOrNSDictionary:responseObject];
        
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
    
    
}


- (void)data{
    // 积分
    AFHTTPSessionManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = _username;
    

    
    NSDictionary *param = @{@"username":username,
                            @"usertype":@1,
                            @"fkfs":@3,
                            @"p":@1};
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        //     NSLog(@"--积分-------%@", responseObject);
        //        NSLog(@"dayyyy");
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
    //       NSLog(@"dataArr--%@", string);
        
        self.dataArr = [self toArrayOrNSDictionary:responseObject];
        
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}




- (void)addUsertopUI {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    
    self.titleLabel.text = @"消费记录";
    
}

- (void)gobackClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)addMyOrdertableview {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT*0.16, WIDTH, HEIGHT*0.84) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.rowHeight = 80;
    [self.view addSubview:self.tableView];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

      recJiFenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recJiFenTableViewCell"];
            if (!cell) {
                cell = [[[NSBundle mainBundle]loadNibNamed:@"recJiFenTableViewCell" owner:nil options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
    if (BtnState == YES) {
        
                cell.titleID.text = [NSString stringWithFormat:@"产品:%@", [self.dataArr[indexPath.row] objectForKey:@"title"]];
                //  cell.time.text = [NSString stringWithFormat:@"%@", [self.dataArr[indexPath.row] objectForKey:@"jine"]];
                cell.VIPnum.text = [NSString stringWithFormat:@"会员号:                                                                    %@", [self.dataArr[indexPath.row] objectForKey:@"username"]];
                if ([[self.dataArr[indexPath.row] objectForKey:@"zt"] isEqualToString:@"1"]) {
                    cell.onOff.text = [NSString stringWithFormat:@"状态：%@", @"成功"];
                }else{
                    cell.onOff.text = @"失败";
                }
        cell.money.text = self.dataArr[indexPath.row][@"jine"];
// 第三
         //       NSLog(@"111");
    }else{
        

        
        if (orderstate == 1001){
            
            
            
            
        }else{
            
            
            
            
            
        }
        
    }
  

    return cell;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (orderstate == 1000) {
        return 5;
    } else if (orderstate == 1001){
        return 5;
    } else if (orderstate == 1002){
        return 5;
    }else {
        return 5;
    }
}




- (void)getdata {
    // 获取当前账号
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    }else {
             _username = [dic[@"data"] objectForKey:@"username"];
    }
    
}





// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
